package com.taobao.freetest.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.taobao.freetest.Activator;
import com.taobao.freetest.ui.editer.Editor;
import com.taobao.freetest.ui.editer.FreeTestUrlEditor;
import com.taobao.freetest.ui.editer.UserEditor;

public class UserPreference extends FieldEditorPreferencePage implements IWorkbenchPreferencePage{

	private List<Editor> allEditors = new ArrayList<Editor>();

	@Override
	public boolean performOk() {
		if(isValid()){
			return super.performOk();
		}else{
			return false;
		}
	}
	
	@Override
	public void setValid(boolean b) {
		if(!b){
			super.setValid(false);
			return;
		}
		for (Editor cs : allEditors) {
			if(!cs.isOk()){
				super.setValid(false);
				return;
			}
		}
		super.setValid(true);
	}
	
	

	@Override
	protected void createFieldEditors() {
		UserEditor userField = new UserEditor(getFieldEditorParent(),this);
		addField(userField);
		FreeTestUrlEditor freetestUrlField = new FreeTestUrlEditor(getFieldEditorParent(),this);
		addField(freetestUrlField);
		
		allEditors.add(userField);
		allEditors.add(freetestUrlField);
	}
	

	@Override
	protected IPreferenceStore doGetPreferenceStore() {
			return Activator.getDefault().getPreferenceStore();
	}
	@Override
	public void init(IWorkbench workbench) {
	}

}
